var e = require("../../@babel/runtime/helpers/interopRequireDefault").default;
Object.defineProperty(exports, "__esModule", { value: !0 }),
  (exports.default = void 0);
var t = require("../../@babel/runtime/helpers/classCallCheck"),
  a = require("../../@babel/runtime/helpers/createClass"),
  r = require("../../@babel/runtime/helpers/inherits"),
  n = require("../../@babel/runtime/helpers/createSuper"),
  i = (function (e) {
    r(s, e);
    var i = n(s);
    function s() {
      var e;
      t(this, s),
        ((e = i.call(this)).started = !1),
        (e.beacons = []),
        (e.restartTimer = void 0),
        (e._lastStatus = void 0),
        (e._lastBtStatus = void 0);
      var a = wx.getSystemInfoSync(),
        r = a.platform;
      return (
        (e.restartTime = "android" === r ? 3e3 : 24e5),
        (e.onBeaconUpdateHandler = function (t) {
          e.stopBeaconRestartTimer();
          var a = t.beacons;
          s.VALID_MAJORS.size > 0 &&
            (a = a.filter(function (e) {
              return s.VALID_MAJORS.has(e.major);
            })),
            a.forEach(function (t) {
              if (0 !== t.rssi && t.rssi > -100) {
                var a = -1;
                for (var r in e.beacons) {
                  var n = e.beacons[r];
                  if (n.major === t.major && n.minor === t.minor) {
                    a = r;
                    break;
                  }
                }
                a >= 0 ? (e.beacons[a] = t) : e.beacons.push(t);
              }
            });
        }),
        (e.onBeaconServiceChangeHandler = function (t) {
          t.available !== e._lastStatus && t.available && e.start(),
            (e._lastStatus = t.available),
            e.emitBtStatus(t.available);
        }),
        (e.resultTimer = setInterval(function () {
          e.started &&
            (e.emit("bt_scanresult", {
              value: e.beacons,
              timestamp: new Date().getTime(),
            }),
            e.beacons.length > 0
              ? (e.beacons = [])
              : void 0 === e.restartTimer &&
                (e.restartTimer = setTimeout(function () {
                  e.start(!0);
                }, e.restartTime)));
        }, s.interval)),
        e
      );
    }
    return (
      a(
        s,
        [
          {
            key: "start",
            value: function () {
              var e = this,
                t =
                  arguments.length > 0 &&
                  void 0 !== arguments[0] &&
                  arguments[0],
                a = !t && this.started;
              return (
                (this.started = !0),
                new Promise(function (t) {
                  a
                    ? t()
                    : e.stop(!0, !0).then(function () {
                        e.stopBeaconTimer(), e.startScan().then(t);
                      });
                })
              );
            },
          },
          {
            key: "stop",
            value: function (e, t) {
              return void 0 !== e && e
                ? (t || (this.started = !1),
                  this.stopBeaconTimer(),
                  this.stopScan())
                : new Promise(function (e) {
                    e();
                  });
            },
          },
          {
            key: "stopBeaconTimer",
            value: function () {
              (this.beacons = []), this.stopBeaconRestartTimer();
            },
          },
          {
            key: "stopBeaconRestartTimer",
            value: function () {
              void 0 !== this.restartTimer &&
                (clearTimeout(this.restartTimer), (this.restartTimer = void 0));
            },
          },
          {
            key: "checkBtEnable",
            value: function (e) {
              var t = !1;
              if (void 0 !== e.errCode && null !== e.errCode)
                switch (e.errCode) {
                  case 0:
                  case 11003:
                    t = !0;
                }
              else if (void 0 !== e.errMsg && null !== e.errMsg) {
                var a = e.errMsg.toLocaleLowerCase().replace(/\s+/g, "");
                (-1 !== a.indexOf("ok") || -1 !== a.indexOf("alreadystart")) &&
                  (t = !0);
              }
              this.emitBtStatus(t);
            },
          },
          {
            key: "emitBtStatus",
            value: function (e) {
              var t = { oldStatus: 0, newStatus: 0 };
              e ? (t.newStatus = 1) : (t.oldStatus = 1),
                (this._lastBtStatus = t),
                this.emit("bt_statusChanged", {
                  value: t,
                  timestamp: new Date().getTime(),
                });
            },
          },
          {
            key: "init",
            value: function () {
              this.deinit(),
                wx.onBeaconUpdate(this.onBeaconUpdateHandler),
                wx.onBeaconServiceChange(this.onBeaconServiceChangeHandler);
            },
          },
          {
            key: "deinit",
            value: function () {
              wx.offBeaconUpdate &&
                wx.offBeaconUpdate(this.onBeaconUpdateHandler),
                wx.offBeaconServiceChange &&
                  wx.offBeaconServiceChange(this.onBeaconServiceChangeHandler);
            },
          },
          {
            key: "getLastBtStatus",
            value: function () {
              return this._lastBtStatus;
            },
          },
          {
            key: "startScan",
            value: function () {
              var e = this;
              return new Promise(function (t) {
                wx.startBeaconDiscovery({
                  uuids: s.UUID_LIST,
                  ignoreBluetoothAvailable: !0,
                  complete: function (a) {
                    e.started ? (e.init(), e.checkBtEnable(a)) : e.stop(!0),
                      t();
                  },
                  success: function (t) {
                    e.emit("bt_scansuccess");
                  },
                  fail: function (t) {
                    e.emit("bt_error", {
                      value: "device not ready",
                      timestamp: new Date().getTime(),
                    });
                  },
                });
              });
            },
          },
          {
            key: "stopScan",
            value: function () {
              return (
                this.deinit(),
                new Promise(function (e) {
                  wx.stopBeaconDiscovery({
                    complete: function (t) {
                      e();
                    },
                  });
                })
              );
            },
          },
        ],
        [
          {
            key: "resetUUID",
            value: function () {
              return (
                (this.UUID_LIST = this.DEFAULT_UUID_LIST.map(function (e) {
                  return e;
                })),
                this.UUID_LIST
              );
            },
          },
        ],
      ),
      s
    );
  })(e(require("../common/EventDispatcher.js")).default);
(i.DEFAULT_UUID_LIST = [
  "a99bc9df-84ae-418c-bf7b-2dd963fe109f",
  "fc8c3ed9-59da-4912-81d3-785a03adbb53",
  "fda50693-a4e2-4fb1-afcf-c6eb07647825",
  "ab8190d5-d11e-4941-acc4-42f30510b408",
]),
  (i.UUID_LIST = []),
  (i.VALID_MAJORS = new Set()),
  (i.interval = 800),
  i.resetUUID();
var s = i;
exports.default = s;
